﻿<?php

class LightController extends Controller
{
	public function actionSetdata(){
	    $comport = $_REQUEST['port_num'];
	    $data = $_REQUEST['data'];
	    $data_str = $comport.','.$data;
	    exec("ComPort set ".$data_str,$result);
	    if($result[0]=="SUCCESS"){
	        $result = json_encode(array('status'=>1,'info'=>$data_str,'msg'=>'发送设置成功'));
	    }else{
	        $result = json_encode(array('status'=>0,'info'=>$data_str,'msg'=>'程序执行出错'));
	    }    
	    echo $result;
	    exit();
	}
    
	public function actionGetport(){
	    $result = array();
	    $port = array();
	    exec("ComPort get",$result);
	    if(!empty($result)){
	    	for($i=1;$i<count($result);$i++){
	    	    $index = substr($result[$i],3);
	    	    $port[$index] = $result[$i];
	    	}
	    	$result = json_encode(array('status'=>1,'info'=>$port,'msg'=>'获取成功'));
	    }else{
	        $result = json_encode(array('status'=>0,'info'=>'','msg'=>'服务器无串口'));
	    }
	    echo $result;
	    exit();
	}
	
	public function actionComport(){
	    $data_str = $this->getData();
	    if(!empty($_REQUEST['set'])){
    	    $result = array();
    		exec("ComPort ".$data_str,$result);
    		if($result[0]=="SUCCESS"){
    			echo "设置成功";
    		}else{
    			echo "程序执行出错";
    		}
	    }
		$this->render('port');
	}
	
	
	private function getData(){
		/*
	    $serial_port = $_REQUEST['port_name'];
	    $data_string = $_REQUEST['send_data'];
	    $data_str = $serial_port.','.$data_string;
	    */
	    
	    //random_data
	    $data = array();
	    for($i=0;$i<500;$i++){
	        $data[] = 0;
	    }
	    $data[0]=128;
	    $data[1]=32;
	    $data[2]=64;
	    $data_str = 3;
	    foreach($data as $key=>$value){
	        $data_str .= ",".$value;
	    }
	    return $data_str;
	}
	
	public function actionTest(){
		$data_str = $this->getRandomData();
		$this->sendDataByDio(3,$data_str);
	}
	
	public function actionTest2(){
	    $data_str = $this->getRandomData();
	    $this->sendDataBySer(3,$data_str);
	}
	
	private function getRandomData(){
    	$data = array();
        for($i=0;$i<500;$i++){
        	$data[] = 0;
        }
        $data[0]=128;
        $data[1]=32;
        $data[2]=64;
        $data_str = '';
        foreach($data as $key=>$value){
        	$data_str .= strval(chr($value));
        }
        return $data_str;
	}
	
	
	private function sendDataBySer($com_flag,$data_str){
	    echo ser_open("COM".$com_flag, 250000, 8, "None", "2", "None");
	    if (ser_isopen() == true ){
	        $str_1 = 0;
	        $str_1  = strval(chr($str_1));
	        ser_write($str_1);
	        ser_close();
	        ser_open("COM".$com_flag, 250000, 8, "Mark", "1", "None");
	        $str_1 = 0;
	        $str_1  = strval(chr($str_1));
	        ser_write($str_1);
	        ser_write($data_str);
	        ser_close();
	    }
	    
	}
	
	
	private function sendDataByDio($com_flag,$data_str){
	    $result_space =array();
	    exec('mode COM'.$com_flag.': baud=250000 data=8 stop=2 parity=n xon=off',$result_space);
	    $com_open = dio_open("COM".$com_flag, O_RDWR);
	    var_dump($result_space);
	    $com = dio_stat($com_open);
	    $str_1 = 0;
	    $str_1  = strval(chr($str_1));
	    dio_write($com_open , $str_1);
	    dio_close($com_open);
	    
	    exec('mode COM'.$com_flag.': baud=250000 data=8 stop=1 parity=m xon=off',$result_mark);
	    $com_open = dio_open("COM".$com_flag, O_RDWR);
	    $com = dio_stat($com_open);
	    var_dump($result_mark);
	    $str_2 = 0;
	    $str_2  = strval(chr($str_2));
	    dio_write($com_open , $str_2);
	    $write_length = dio_write($com_open , $data_str);
	    if($write_length){
	        echo json_encode(array('status'=>1,'info'=>$write_length,'msg'=>'Write Success!'));
	    } else{
	        echo json_encode(array('status'=>0,'info'=>$write_length,'msg'=>'Write Failed!'));
	    }
	    dio_close($com_open);
	}
	
	
}